package com.example.jetpackdemoapp.ui.home.activities

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.JetpackDemoApp.R
import com.example.JetpackDemoApp.databinding.ActivityRecyclerViewBinding
import com.example.jetpackdemoapp.tools.CommonUtil

class RecyclerViewActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRecyclerViewBinding
    private lateinit var tableDatas: MutableList<ViewModel>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRecyclerViewBinding.inflate(layoutInflater)
        setContentView(binding.root)

        loadDatas()
        setupViews()
    }

    private fun loadDatas() {
        tableDatas = arrayListOf(
            ViewModel(icon = "icon_cal", title = "one"),
            ViewModel(icon = "icon_pwd", title = "two"),
            ViewModel(icon = "icon_cal", title = "three"),
            ViewModel(icon = "icon_pwd", title = "four"),
            ViewModel(icon = "icon_phone", title = "five"),
            ViewModel(icon = "icon_cal", title = "six"),
            ViewModel(icon = "icon_pwd", title = "seven"),
            ViewModel(icon = "icon_cal", title = "eight"),
            ViewModel(icon = "icon_cal", title = "night"),
            ViewModel(icon = "icon_pwd", title = "ten"),
            ViewModel(icon = "icon_cal", title = "eleven"),
            ViewModel(icon = "icon_cal", title = "twelve"),
            ViewModel(icon = "icon_cal", title = "thirteen"),
            ViewModel(icon = "icon_", title = "fourteen"),
            ViewModel(icon = "icon_cal", title = "fifteen"),
            ViewModel(icon = "icon_cal", title = "sixteen"),
            ViewModel(icon = "icon_cal", title = "seventeen"),
            ViewModel(icon = "icon_cal", title = "eighteen"),
            ViewModel(icon = "icon_cal", title = "nineteen"),
            ViewModel(icon = "icon_cal", title = "twenty")
        )
    }

    private fun setupViews() {
        binding.homeRecyclerView01.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        binding.homeRecyclerView01.adapter = CustomerAdapter()
    }

    private inner class CustomerAdapter : RecyclerView.Adapter<CustomerAdapter.ViewHolder>() {
        private inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            var iconImageView: ImageView
            var titleTextView: TextView

            init {
                iconImageView = view.findViewById(R.id.item_view_linear_layout_icon)
                titleTextView = view.findViewById(R.id.item_view_linear_layout_text)
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_view_linear_vertical, parent, false)
            return ViewHolder(itemView)
        }

        override fun getItemCount(): Int {
            return tableDatas.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val item = tableDatas[position]
            var resImgId: Int = CommonUtil.getResId(variableName = item.icon, clazz = R.mipmap::class.java)
            if(resImgId == -1) {
                resImgId = R.mipmap.icon_cal
            }
            holder.iconImageView.setImageResource(resImgId)
            holder.titleTextView.text = item.title
        }
    }

    private inner class ViewModel(
        val icon: String,
        val title: String
    )
}